package com.walljiang.ffvideohandle;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.SoftReference;

public class MainActivity extends AppCompatActivity implements Handler.Callback, View.OnClickListener {
    private final String TAG = "fflog";

    TextView tvHello;

    Button btn_confirm;

    EditText et_endPosition,et_startPosition;

    HandlerThread handlerThread;

    static BackHandler backHandler;

    Handler uiHandler;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_confirm:
                Log.i(TAG,"btn_confirm onClick");
                Message aCutMsg = backHandler.obtainMessage(MSG_BACK_CUT_AUDIO);
                String startStr = et_startPosition.getText().toString();
                String endStr = et_endPosition.getText().toString();
                if(!TextUtils.isEmpty(startStr) && !TextUtils.isEmpty(endStr)){
                    Log.i(TAG,"btn_confirm sendToTarget");
                    aCutMsg.arg1 = Integer.parseInt(startStr);
                    aCutMsg.arg2 = Integer.parseInt(endStr);
                    aCutMsg.obj = "hahaha";
                    aCutMsg.sendToTarget();
                }
                break;
        }
    }

    class BackHandler extends Handler{
        SoftReference<MainActivity> softReference;

        public BackHandler(MainActivity mainActivity, Looper looper){
            super(looper);
            softReference = new SoftReference<MainActivity>(mainActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(softReference != null){
                MainActivity activity = softReference.get();
                if(activity != null){
                    activity.handleBackMessage(msg);
                }
            }
        }

        public void release(){
            getLooper().quit();
            softReference.clear();
        }
    }

    private final int MSG_BACK_CUT_AUDIO = 0x01;

    public void handleBackMessage(Message msg){
        switch (msg.what){
            case MSG_BACK_CUT_AUDIO:
                Log.i(TAG,"MSG_BACK_CUT_AUDIO"+msg.arg1+":"+msg.arg2);
                int result = JNIProxy.getInstance().audioCut((String) msg.obj,(long)msg.arg1,(long)msg.arg2);
                Log.i(TAG,"MSG_BACK_CUT_AUDIO MSG_UI_REFRESH_CUT_AUDIO");
                Message rMsg = uiHandler.obtainMessage(MSG_UI_REFRESH_CUT_AUDIO);
                rMsg.obj = result;
                rMsg.sendToTarget();
                break;
        }
    }

    private final int MSG_UI_REFRESH_CUT_AUDIO = 0x01;

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what){
            case MSG_UI_REFRESH_CUT_AUDIO:
                Log.i(TAG,"MSG_UI_REFRESH_CUT_AUDIO");
                Object obj = msg.obj;
                if(obj != null){
                    Log.i(TAG,"MSG_UI_REFRESH_CUT_AUDIO REFRESH");
                    int result = (int) obj;
                    et_endPosition.setText("");
                    et_startPosition.setText("");
                    if(result >= 0){
                        Toast.makeText(MainActivity.this,"截取成功",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(MainActivity.this,"截取失败",Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        uiHandler = new Handler(this);
        handlerThread = new HandlerThread(TAG);
        handlerThread.start();
        backHandler = new BackHandler(this,handlerThread.getLooper());

        tvHello = (TextView) findViewById(R.id.tv_hello);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(this);
        et_startPosition = (EditText) findViewById(R.id.et_startPosition);
        et_endPosition = (EditText) findViewById(R.id.et_endPosition);

        String hellojni = JNIProxy.getInstance().helloJni();
        tvHello.setText(hellojni==null?"hello jni == null":hellojni);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(backHandler != null){
            backHandler.release();
        }
    }
}
